<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item
            :label="$t('accountManage.tenantAccountNumber') /*租户账号*/"
          >
            <el-input
              v-model.trim="queryForm.keyword1"
              :placeholder="
                $t('accountManage.tenantAccountNumber') /*请输入租户账号*/
              "
              clearable
            />
          </el-form-item>
          <el-form-item
            :label="$t('accountManage.agentAccountNumber') /*代理商账号*/"
          >
            <el-input
              v-model.trim="queryForm.keyword2"
              :placeholder="
                $t(
                  'accountManage.pleaseEditAgentAccountNumber'
                ) /*请输入代理商账号*/
              "
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ $t('common.search') /*查询*/ }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="24">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit('', 'NewAccountManagementEdit', 0)"
        >
          {{ $t('common.newBug') /*新购*/ }}
        </el-button>
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit('', 'NewAccountManagementEdit', 1)"
        >
          {{ $t('common.renewal') /*续费*/ }}
        </el-button>
        <el-button
          icon="el-icon-delete"
          type="danger"
          @click="handleDelete('', 1)"
        >
          {{ $t('common.disable') /*禁用*/ }}
        </el-button>
        <el-button
          icon="el-icon-delete"
          type="danger"
          @click="handleDelete('', 0)"
        >
          {{ $t('common.urgentNeed') /*启用*/ }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="username"
        :label="$t('accountManage.tenantAccountNumber') /*租户账号*/"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="count_dev"
        :label="$t('accountManage.equipmentAccounts') /*设备账号数量*/"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="mark"
        :label="$t('accountManage.remark') /*备注*/"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        :label="$t('accountManage.status') /*状态*/"
      >
        <template #default="{ row }">
          <el-tag>
            {{ setStatusName(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="fm_end_time"
        :label="$t('accountManage.expirationTime') /*过期时间*/"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="agent_name"
        :label="$t('accountManage.agentAccountNumber') /*代理商账号*/"
      ></el-table-column>
      <!-- <el-table-column
        show-overflow-tooltip
        prop="email"
        label="邮箱"
      ></el-table-column> -->

      <el-table-column show-overflow-tooltip label="操作" width="400">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ $t('accountManage.edit') /*编辑*/ }}
          </el-button>
          <el-button type="text" @click="handleEdit(row, 'passwordReset')">
            {{ $t('accountManage.passwordReset') /*密码重置*/ }}
          </el-button>
          <el-button
            type="text"
            @click="handleEdit(row, 'deviceAccountManage')"
          >
            {{ $t('accountManage.deviceAccountManage') /*设备账号管理*/ }}
          </el-button>
          <!-- <el-button type="text" @click="handleDelete(row)">
            {{ $t('accountManage.delete') /*删除*/ }}
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.limit"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <edit ref="edit" @fetch-data="fetchData"></edit>
    <new-account-management-edit
      ref="NewAccountManagementEdit"
      @fetch-data="fetchData"
    />
    <password-reset ref="passwordReset" @fetch-data="fetchData" />
    <!-- 设备账号管理 -->
    <device-account-manage ref="deviceAccountManage" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getTenentAccountList, setUserDelete } from '@/api/agents.js'
  import DeviceAccountManage from '@/views/accountManagement/userManagement/components/accountManage.vue'
  import NewAccountManagementEdit from '@/views/accountManagement/userManagement/components/newAccountManagementEdit.vue'
  import PasswordReset from '@/views/accountManagement/userManagement/components/passwordReset.vue'
  import Edit from './components/UserManagementEdit'
  export default {
    name: 'UserManagement',
    components: {
      Edit,
      NewAccountManagementEdit,
      PasswordReset,
      DeviceAccountManage,
    },
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          page: 1,
          limit: 10,
          keyword1: '',
          keyword2: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setStatusName(status) {
        let statusName = '启用'
        if (status === 1) {
          statusName = '禁用'
        } else if (status === 2) {
          statusName = '过期'
        }
        return statusName
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row, type = 'edit', editType = 0) {
        if (row.user_id) {
          this.$refs[type].showEdit(row, editType, this.fetchData)
        } else {
          this.$refs[type].showEdit('', editType, this.fetchData)
        }
      },
      handleDelete(row, is_delete) {
        if (row?.user_id) {
          this.$baseConfirm(
            `${this.$t(
              'accountManage.areYouSureDeleteCurrent'
            )}` /*你确定要删除当前项吗*/,
            null,
            async () => {
              const { msg } = await setUserDelete({ user_list: row.user_id })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            }
          )
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.user_id)
            let message =
              is_delete === 0
                ? `${this.$t(
                    'accountManage.areYouSureNotForbitSel'
                  )}` /*你确定要启用选中项吗*/
                : `${this.$t(
                    'accountManage.areYouSureForbitSel'
                  )}` /*你确定要禁用选中项吗*/
            this.$baseConfirm(message, null, async () => {
              const { msg } = await setUserDelete({
                user_list: ids,
                is_delete: is_delete.toString(),
              })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            })
          } else {
            this.$baseMessage(
              `${this.$t('accountManage.notSelAnyRow')}` /*未选中任何行*/,
              'error'
            )
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.limit = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.page = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        // const { data, totalCount } = userData || (await getList(this.queryForm))
        const { data } = await getTenentAccountList(this.queryForm)
        // const { total } = data
        console.log(data, '获取数据')
        this.list = data?.data || []
        this.total = data?.total || 0
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
